package com.saxon.system.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 登录权限控制
 *
 * @author saxon
 */
@Data
@ApiModel (value = "权限认证对象", description = "权限认证")
public class SysAuthUser implements UserDetails, Serializable {

    @ApiModelProperty (value = "表id")
    @TableId (value = "id", type = IdType.AUTO)
    private Integer id;

    @ApiModelProperty (value = "用户密码")
    private String password;
    @ApiModelProperty (value = "用户姓名")
    private String name;
    @ApiModelProperty (value = "用户权限")
    private String privilege;
    @ApiModelProperty (value = "用户是否过期")
    private int expired;
    @ApiModelProperty (value = "用户是否被锁定")
    private int locked;
    @ApiModelProperty (value = "用户是否可用")
    private int active;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities () {
        List<GrantedAuthority> list = new ArrayList<> ();
        list.add (new SimpleGrantedAuthority (privilege));
        return list;
    }

    @Override
    public String getPassword () {
        return password;
    }

    @Override
    public String getUsername () {
        return name;
    }

    @Override
    public boolean isAccountNonExpired () {
        return expired == 0;
    }

    @Override
    public boolean isAccountNonLocked () {
        return locked == 0;
    }

    @Override
    public boolean isCredentialsNonExpired () {
        return true;
    }

    @Override
    public boolean isEnabled () {
        return active == 1;
    }
}
